package cn.qh.seasonal.service.impl;

import cn.qh.seasonal.entity.Factory;
import cn.qh.seasonal.service.FactoryService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.qh.seasonal.mapper.ProductionMapper;
import cn.qh.seasonal.entity.Production;
import cn.qh.seasonal.service.ProductionService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * (Production)表服务实现类
 *
 * @author makejava
 * @since 2021-11-11 16:45:41
 */
@Service("productionService")
public class ProductionServiceImpl extends ServiceImpl<ProductionMapper, Production> implements ProductionService {

    @Resource
    private FactoryService factoryService;

    @Resource
    private ProductionMapper productionMapper;

    @Override
    public Page<Production> ProductionBizAll(int factoryId, String startDate, String EndDate) {
        //通过传过来的工厂id查询工厂信息
        Factory byId = factoryService.getById(factoryId);
        //自己写的方法 通过工厂id 起始时间 结束时间 查询
        List<Production> productionPage = productionMapper.ProductionBizAll(factoryId, startDate, EndDate);
        Page page = new Page();
        page.setCurrent(1);
        page.setSize(3);
        page.setRecords(productionPage);


        return page;
    }

    @Override
    public Page<Production> ProductionBizAllweekDay(String weekdayId, int factoryId) {
        Page page = new Page();
        page.setCurrent(1);
        page.setSize(3);
        page.setRecords(productionMapper.ProductionBizAllweekDay(weekdayId, factoryId));
        return page;
    }

    public static String[] getDates(String dateFrom, String dateEnd, String weekDays) {
        long time = 1l;
        long perDayMilSec = 24 * 60 * 60 * 1000;
        List<String> dateList = new ArrayList<String>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        //需要查询的星期系数
        String strWeekNumber = weekForNum(weekDays);
        try {
            dateFrom = sdf.format(sdf.parse(dateFrom).getTime() - perDayMilSec);
            while (true) {
                time = sdf.parse(dateFrom).getTime();
                time = time + perDayMilSec;
                Date date = new Date(time);
                dateFrom = sdf.format(date);
                if (dateFrom.compareTo(dateEnd) <= 0) {
                    //查询的某一时间的星期系数
                    Integer weekDay = dayForWeek(date);
                    //判断当期日期的星期系数是否是需要查询的
                    if (strWeekNumber.indexOf(weekDay.toString()) != -1) {
                        System.out.println(dateFrom);
                        dateList.add(dateFrom);
                    }
                } else {
                    break;
                }
            }
        } catch (ParseException e1) {
            e1.printStackTrace();
        }
        String[] dateArray = new String[dateList.size()];
        dateList.toArray(dateArray);
        return dateArray;
    }

    //等到当期时间的周系数。星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static Integer dayForWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return calendar.get(Calendar.DAY_OF_WEEK);
    }

    /**
     * 得到对应星期的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
     *
     * @param weekDays 星期格式  星期一|星期二
     */
    public static String weekForNum(String weekDays) {
        //返回结果为组合的星期系数
        String weekNumber = "";
        //解析传入的星期
        if (weekDays.indexOf("|") != -1) {//多个星期数
            String[] strWeeks = weekDays.split("\\|");
            for (int i = 0; i < strWeeks.length; i++) {
                weekNumber = weekNumber + "" + getWeekNum(strWeeks[i]).toString();
            }
        } else {//一个星期数
            weekNumber = getWeekNum(weekDays).toString();
        }

        return weekNumber;

    }

    //将星期转换为对应的系数  星期日：1，星期一：2，星期二：3，星期三：4，星期四：5，星期五：6，星期六：7
    public static Integer getWeekNum(String strWeek) {
        Integer number = 1;//默认为星期日
        if ("7".equals(strWeek)) {
            number = 1;
        } else if ("1".equals(strWeek)) {
            number = 2;
        } else if ("2".equals(strWeek)) {
            number = 3;
        } else if ("3".equals(strWeek)) {
            number = 4;
        } else if ("4".equals(strWeek)) {
            number = 5;
        } else if ("5".equals(strWeek)) {
            number = 6;
        } else if ("6".equals(strWeek)) {
            number = 7;
        }
        return number;
    }
}

